#! /usr/bin/perl
package DBInfoSubs;

#use Db
use DBQueries;
use strict;

#subroutine to update the user information
#this will be called whenever a file is added or deleted, so that the user stats can be updated
sub update_user_stats{
	
	#obtain the username from parameter
	my $username = $_[1];
	
	#get all the files of teh user
	my @user_files = DBQueries::getUserFileNames($username);
	
	
	#NUMBER OF FILES
	my $user_file_count = scalar(@user_files);
	
	#AVERAGE FILE SIZE
	my $average_file_size;
	
	#ensure that ther eare files
	if ($user_file_count > 0){
		
		#print $user_file_count;
		
		#average file size
		my $total_file_size = 0;
		my $current_file_id;
		
		#temp var to hold array passed form query
		my @current_total_file_size;
	
		#loop to average
		foreach(@user_files){
		
			$current_file_id = DBQueries::getFileID($username, $_);
			
			#add that size to total size
			#print "$current_file_id\n";
			@current_total_file_size = DBQueries::getInfoType($current_file_id, "fileSize");
			$total_file_size += $current_total_file_size[0];
		}
	
		#divide
		$average_file_size = $total_file_size / $user_file_count;
	}
	#otherwise set to N.A.
	else{
		$average_file_size = "N.A.";
	}
	
	#add to DB
	DBQueries::removeInfoType($username, "averageFileSize"); 
	DBQueries::addInfo($username, "averageFileSize", $average_file_size);
	
	#AVERAGE WORD COUNT
	my $average_word_count;
	#ensure that ther eare files
	if ($user_file_count > 0){
		
		#average file size
		my $total_word_count = 0;
		my $current_file_id;
		
		#temp
		my @current_total_word_count;
	
		#loop to average
		foreach(@user_files){
		
			$current_file_id = DBQueries::getFileID($username, $_);
			
			#add that size to total size
			@current_total_word_count = DBQueries::getInfoType($current_file_id, "wordCount");
			$total_word_count += $current_total_word_count[0];
		}
	
		#divide
		$average_word_count = $total_word_count / $user_file_count;
	}
	#otherwise set to N.A.
	else{
		$average_word_count = "N.A.";
	}	
	
	#add to DB
	DBQueries::removeInfoType($username, "averageWordCount");
	DBQueries::addInfo($username, "averageWordCount", $average_word_count);
	
	#AVERAGE WORD SIZE
	my $average_word_size;
	#ensure that ther eare files
	if ($user_file_count > 0){
		
		#average file size
		my $total_word_size = 0;
		my $current_file_id;
		
		#temp
		my @current_averge_word_size;
	
		#loop to average
		foreach(@user_files){
		
			$current_file_id = DBQueries::getFileID($username, $_);
			
			#add that size to total size
			@current_averge_word_size = DBQueries::getInfoType($current_file_id, "avgWordLength");
			$total_word_size += $current_averge_word_size[0];
		}
	
		#divide
		$average_word_size = $total_word_size / $user_file_count;
	}
	#otherwise set to N.A.
	else{
		$average_word_size = "N.A.";
	}	
	
	#add to DB
	DBQueries::removeInfoType($username, "averageWordLength");
	DBQueries::addInfo($username, "averageWordLength", $average_word_size);
	
	#TOP 30 WORDS
	
	my $current_id;
	
	#declare hash to hold word count
	my %word_count = ();
	
	#loop to get ids adn then get infomration
	foreach (@user_files){
		
		#get id of name
		$current_id = DBQueries::getFileID($username, $_);
		
		my @current_keywords = DBQueries::getInfoTypeValue($current_id, "keyword") ;
		
		#declare variables to temporarily hold key and value
		my($keyword, $value);
		
		#now loop through all keyword value pairs and process all infomration passed by teh DB
		foreach my $pair (@current_keywords){

			#split around delimeter sign
			my ($key, $value) = split(/\.\|><\|\./, $pair);

			#see if the key already exists and creat it if it doesnt
			if(exists($word_count{"$key"})){
				
				#in that case simply add
				$word_count{"$key"} += $value;
			}
			#otherwise create
			else{
				$word_count{"$key"} = $value;
			}
		} # end foreach to DB info
		
		#now sort
		my @sorted_words = sort{$word_count{$b} <=> $word_count{$a}} keys %word_count;
		
		#clear the DB first
		DBQueries::removeInfoType($username, "topWord");
		
		#loop all and add to user
		my $loop_counter;
		#determine correct number of iterations
		if(scalar(@sorted_words) < 30){
			$loop_counter = scalar(@sorted_words);
		}
		else{
			$loop_counter = 30;
		}
		
		#now add the top 30 to the DB
		for(my $i = 0; $i<$loop_counter; $i++){
			
			
			DBQueries::addInfo($username, "topWord", $sorted_words[$i], $word_count{$sorted_words[$i]});
		}
	}
}

#subroutine to update the group information
#this will be called whenever a file is added or deleted from a group so that the general info can be updated
sub update_group_stats{
	
	#obtain the username from parameter
	my $groupname = $_[1];
	
	my $file_owner = DBQueries::getGroupOwner($groupname);
	
	#get all the files of teh user
	my @group_files = DBQueries::getGroupFileNames($groupname);
	
	#NUMBER OF FILES
	my $group_file_count = scalar(@group_files);
	
	#AVERAGE FILE SIZE
	#ensure that ther eare files
	my $average_file_size;
	if ($group_file_count > 0){
		
		#average file size
		my $total_file_size = 0;
		my $current_file_id;
		
		my @current_total_file_size;
	
		#loop to average
		foreach(@group_files){
		
			$current_file_id = DBQueries::getFileID($file_owner, $_);
			
			#add that size to total size
			@current_total_file_size = DBQueries::getInfoType($current_file_id, "fileSize");
			$total_file_size += $current_total_file_size[0];
		}
	
		#divide
		$average_file_size = $total_file_size / $group_file_count;
		
	}
	#otherwise set to N.A.
	else{
		$average_file_size = "N.A.";
	}
	
	#add to DB
	DBQueries::removeInfoType($file_owner, $groupname, "averageFileSize");
	DBQueries::addInfo($file_owner, $groupname, "averageFileSize", $average_file_size);
		
	#AVERAGE WORD COUNT
	my $average_word_count;
	#ensure that ther eare files
	if ($group_file_count > 0){
		
		#average file size
		my $total_word_count = 0;
		my $current_file_id;
	
		#temp
		my @current_average_word_count;
		
		#loop to average
		foreach(@group_files){
		
			$current_file_id = DBQueries::getFileID($file_owner, $_);
			
			#add that size to total size
			@current_average_word_count = DBQueries::getInfoType($current_file_id, "wordCount");
			$total_word_count += $current_average_word_count[0];
		}
	
		#divide
		$average_word_count = $total_word_count / $group_file_count;
	}
	#otherwise set to N.A.
	else{
		$average_word_count = "N.A.";
	}	
	
	#add to DB
	DBQueries::removeInfoType($file_owner, $groupname, "averageWordCount");
	DBQueries::addInfo($file_owner, $groupname, "averageWordCount", $average_word_count);
	
	#AVERAGE WORD SIZE
	my $average_word_size;
	#ensure that ther eare files
	if ($group_file_count > 0){
		
		#average file size
		my $total_word_size = 0;
		my $current_file_id;
		
		#temp
		my @current_total_word_size;
	
		#loop to average
		foreach(@group_files){
		
			$current_file_id = DBQueries::getFileID($file_owner, $_);
			
			#add that size to total size
			@current_total_word_size = DBQueries::getInfoType($current_file_id, "avgWordLength");
			$total_word_size += $current_total_word_size[0];
		}
	
		#divide
		$average_word_size = $total_word_size / $group_file_count;
	}
	#otherwise set to N.A.
	else{
		$average_word_size = "N.A.";
	}	
	
	#add to DB
	DBQueries::removeInfoType($file_owner, $groupname, "averageWordLength");
	DBQueries::addInfo($file_owner, $groupname, "averageWordLength", $average_word_size);
	
	#TOP 30 WORDS
	
	my $current_id;
	
	#declare hash to hold word count
	my %word_count = ();
	
	#loop to get ids adn then get infomration
	foreach (@group_files){
		
		#get id of name
		$current_id = DBQueries::getFileID($file_owner, $_);
		
		my @current_keywords = DBQueries::getInfoTypeValue($current_id, "keyword") ;
		
		#declare variables to temporarily hold key and value
		my($keyword, $value);
		
		#now loop through all keyword value pairs and process all infomration passed by teh DB
		foreach my $pair (@current_keywords){

			#split around delimeter sign
			my ($key, $value) = split(/\.\|><\|\./, $pair);

			#see if the key already exists and creat it if it doesnt
			if(exists($word_count{"$key"})){
				
				#in that case simply add
				$word_count{"$key"} += $value;
			}
			#otherwise create
			else{
				$word_count{"$key"} = $value;
			}
		} # end foreach to DB info
		
		#now sort
		my @sorted_words = sort{$word_count{$b} <=> $word_count{$a}} keys %word_count;
		
		#clear the DB first
		DBQueries::removeInfoType($file_owner, $groupname, "topWord");
		
		#loop through the first 30 of these
		my $loop_counter;
		#determine correct number of iterations
		if(scalar(@sorted_words) < 30){
			$loop_counter = scalar(@sorted_words);
		}
		else{
			$loop_counter = 30;
		}
		
		#now add the top 30 to the DB
		for(my $i = 0; $i<$loop_counter; $i++){
			
			DBQueries::addInfo($file_owner, $groupname, "topWord", $sorted_words[$i], $word_count{$sorted_words[$i]});
		}
	}
}

#return 1 to ensure true value of module
1;